package com.breakfast.fun.adapter.user;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.common.CommonCart;
import com.breakfast.fun.dao.DaoMaster;
import com.breakfast.fun.dao.ProductCart;
import com.breakfast.fun.dao.ProductCartDao;
import com.breakfast.fun.data.entity.user.UserGrid;
import com.breakfast.fun.R;
import com.breakfast.fun.event.CartEvent;
import com.breakfast.fun.util.L;
import com.breakfast.fun.view.widget.RoundImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hp on 2016/8/23.
 */
public class UserGridAdapter extends BaseListAdapter<UserGrid> {

    private ProductCartDao productCartDao;
    private List<ProductCart> productCarts;
    private int mUnPayCount = 0;

    private CommonCart mCommonCart = new CommonCart();

    public UserGridAdapter(Context context, List<UserGrid> list) {
        super(context, list);
        mCommonCart.init(mContext);
//        productCartDao = DaoMaster.newDevSession(context,"zaofan.db").getProductCartDao();
//        productCarts = productCartDao.loadAll();
        productCarts = mCommonCart.productCartDao.loadAll();
        for (int i = 0; i < productCarts.size(); i++) {
            mCommonCart.totalPrice += productCarts.get(i).getTotalPrice();
            mCommonCart.totalNum += productCarts.get(i).getNum();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_grid, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ivItemUserGrid.setImageResource(getItem(position).getImage());
        holder.tvItemUserGrid.setText(getItem(position).getTitle());

        if (getItem(position).getTitle().equals("余额充值")) {
            holder.rivItemUserGrid.setVisibility(View.VISIBLE);
        }

        if (getItem(position).getTitle().equals("购物车")) {
            if (productCarts.size() > 0) {
                holder.tvItemUserCount.setVisibility(View.VISIBLE);
                holder.tvItemUserCount.setText(mCommonCart.totalNum + "");
            }
        }

        if (getItem(position).getTitle().equals("我的订单")) {
            if (mUnPayCount != 0) {
                holder.tvItemUserCount.setVisibility(View.VISIBLE);
                holder.tvItemUserCount.setText(mUnPayCount + "");
            }
        }


        return convertView;
    }

    public void setUnPayCount(int unPayCount) {
        mUnPayCount = unPayCount;
    }

    static class ViewHolder {
        @Bind(R.id.iv_item_user_grid)
        ImageView ivItemUserGrid;
        @Bind(R.id.tv_item_user_grid)
        TextView tvItemUserGrid;
        @Bind(R.id.ll_item_user_grid)
        LinearLayout llItemUserGrid;
        @Bind(R.id.riv_item_user_grid)
        RoundImageView rivItemUserGrid;
        @Bind(R.id.tv_item_user_count)
        TextView tvItemUserCount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
